package com.jf.springboot.web;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Enumeration;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-12-05
 * Time:13:19
 */
@Controller
@RequestMapping("/request")
public class RequestController {
    @GetMapping("/login")
    public String login(HttpServletRequest request) {
        request.setAttribute("username", "admin");
        //return "forward:ok";
        return "forward:/request/ok";
    }
    @GetMapping("/ok")
    @ResponseBody
    public String ok(@RequestAttribute(value = "username", required = false) String uname) {
        return uname;
    }
    @GetMapping("/register")
    public String register(HttpServletRequest request, Model model, Map<String, Object> modelMap) {
        request.setAttribute("username", "admin");
        model.addAttribute("password", "admin_aaa");
        modelMap.put("address", "beijing");
        return "forward:/request/register_ok";
    }
    @GetMapping("/register_ok")
    @ResponseBody
    public String registerOk(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("id-" + request.getParameter("id"));
        System.out.println("username-" + request.getAttribute("username"));
        System.out.println("password-" + request.getAttribute("password"));
        System.out.println("address-" + request.getAttribute("address"));
        Cookie cookie = new Cookie("username", (String)request.getAttribute("username"));
        response.addCookie(cookie);
        return "success";
    }
}
